package com.cqyc.imview.activity

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.security.identity.ResultData
import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.cqyc.imview.R
import com.cqyc.imview.StatusBarUtils
import com.cqyc.imview.Utils
import com.cqyc.imview.base.BaseActivity
import com.cqyc.imview.databinding.ActivityHomeSettingBinding
import com.cqyc.imview.model.ConversationModel
import com.cqyc.imview.model.HomeSettingModel
import com.cqyc.network.getid.GetMyInformationBox
import com.cqyc.network.http.DataCallBack
import com.cqyc.network.http.HttpUtil
import com.cqyc.network.model.ConfigInfo
import com.tencent.qcloud.tuikit.tuichat.util.TUIChatLog

class HomeSettingActivity : BaseActivity<ActivityHomeSettingBinding, HomeSettingModel>(),
    View.OnClickListener {

    private val TAG = HomeSettingActivity::class.java.simpleName
    private lateinit var configInfo: ConfigInfo

    override fun setLayout(): Int {
        return R.layout.activity_home_setting
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        TUIChatLog.i(TAG, "onCreate $this")
        super.onCreate(savedInstanceState)
        StatusBarUtils.setStatusBar(window)
        if (GetMyInformationBox.configInfo == null) {
            GetMyInformationBox.configInfo = ConfigInfo("", "", 2)
        }
        configInfo = GetMyInformationBox.configInfo!!
        mBinding.model = mViewModel
        mBinding.onClick = this

        mBinding.mcSwitch.isChecked = configInfo.newMessageSound == 1
        mBinding.mcSwitch.setOnClickListener {
            updateConfig(if (mBinding.mcSwitch.isChecked) "1" else "2")
        }
    }

    private fun updateConfig(str: String) {
        HttpUtil.api.setConfig(str).enqueue(DataCallBack(onResponse = {
            configInfo.newMessageSound = str.toInt()
        }, onFailure = {
            if (it.message == "数据为空") {
                configInfo.newMessageSound = str.toInt()
            } else {
                mBinding.mcSwitch.isChecked = !mBinding.mcSwitch.isChecked
            }
        }))
    }

    override fun onResume() {
        super.onResume()
        mViewModel.systemMsgStatus.value = Utils.isNotificationEnabled(this)
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.ic_back -> {
                finish()
            }
            R.id.notification_layout -> {
                val intent = Intent()
                if (Build.VERSION.SDK_INT >= 26) {
                    // android 8.0引导
                    intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
                    intent.putExtra("android.provider.extra.APP_PACKAGE", packageName)
                } else {
                    // android 5.0-7.0
                    intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
                    intent.putExtra("app_package", packageName)
                    intent.putExtra("app_uid", applicationInfo.uid)
                }
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(intent)
            }
        }
    }

}